package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.FaceUtil;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 用户APP资源控制器
 */
@RestController
@RequestMapping("images")
public class ImageController {
    @PostMapping
    public Map upload(MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();
        String url = null;
        try {
            //把图片传到指定的绝对路径上
            url = UploadUtil.uploadQiniu(file);
            map.put("status", 1);
            map.put("url", url);
        }catch (Exception e){
            e.printStackTrace();
            map.put("status",0);
            map.put("msg","上传失败!");
        }
        return map;
    }
    /**
     * 人脸比较
     * @param localData
     * @return
     */
    @PostMapping("faces")
    public JSONResult upload(String localData){
        JSONResult jsonResult = new JSONResult();
        //随机字符串
        String uuid = UUID.randomUUID().toString();
        //拼接文件名称，不存在就创建
        String imgPath = "E:/face/" + uuid + ".jpg";
        String url = FaceUtil.uploadFile(localData,imgPath);
        //刷脸比较
        boolean b = FaceUtil.compareFace(url);
        if(!b){
            jsonResult.mark("刷脸失败！");
        }
        return jsonResult;
    }

    /**
     * 上传人脸库
     * @param localData
     * @return
     */
    @PostMapping("datas")
    public Map datas(String localData){
        HashMap<String, Object> map = new HashMap<>();
        try{
            //把图片传到指定的绝对路径上
            String imgPath = "E:/software/111.jpg";
            FaceUtil.uploadFile(localData,imgPath);
            map.put("status",1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",0);
            map.put("msg","上传失败!");
        }
        return map;
    }
}
